package com.flysky.controller.finance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.flysky.common.ToolUtil;
import com.flysky.model.common.PagingModel;
import com.flysky.model.finance.BigTypeHbmModel;
import com.flysky.model.finance.FinanceHbmModel;
import com.flysky.model.user.UserHbmModel;
import com.flysky.service.finance.IFinanceService;

@Controller
public class FinanceController {

	Logger flyskyLogger = Logger.getLogger(FinanceController.class);
	
	@Autowired
	private IFinanceService financeService = null;

	public void setFlyskyLogger(Logger flyskyLogger) {
		this.flyskyLogger = flyskyLogger;
	}
	
	@RequestMapping(value="/financeManage.htm")
	public ModelAndView intiFinanceMainPage(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("finance/financeManage");
		return view;
	}
	
	@RequestMapping(value="/financeInput.htm")
	public ModelAndView financeInput(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("finance/financeInput");
		List<BigTypeHbmModel> bigTypeList = this.financeService.getAllBigType();
		view.addObject("bigTypeList", bigTypeList);
		//build parameter
		HashMap<String,String> param = new HashMap<String,String>();
		HttpSession httpSession = request.getSession();
		UserHbmModel userModel =(UserHbmModel)httpSession.getAttribute("user");
		param.put("MAKER", userModel.getUuid());
		//pagination start
		String curPageStr = request.getParameter("curPage");
		if(curPageStr==null){
		curPageStr = (String) request.getAttribute("curPage");
		}
		PagingModel pagingObj = new PagingModel();
		//得到总记录条数
		Long totalCount = this.financeService.getMonthFinanceCounts(param);
		pagingObj.setTotalCount(totalCount);
		if(curPageStr != null){
		Long curPage = Long.parseLong(curPageStr);
		pagingObj.setCurPage(curPage);
		}else{
		pagingObj.setCurPage(1L);
		}
		view.addObject("pagingObj", pagingObj);
		Long startRec = pagingObj.getStartRec();
		//把起始条数传到后台检索数据.
		param.put("START", startRec.toString());
		//pagination end
		List<FinanceHbmModel>  financeList = this.financeService.getAllFinanceItems(param);
		view.addObject("recentNumber",financeList.size());
		view.addObject("financeList", financeList);
		return view;
	}
	
	@RequestMapping(value="/typeManager.htm")
	public ModelAndView initFinanceTypeManager(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView();
		List<BigTypeHbmModel> bigTypeList = this.financeService.getAllBigType();
		view.addObject("bigTypeList", bigTypeList);
		view.setViewName("finance/financeTypeManage");
		return view;
	}
	
	@RequestMapping(value="/addBigType.htm")
	public ModelAndView addBigType(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView();
		String typeName = request.getParameter("typeName");
		this.financeService.addbigType(typeName);
		view.setViewName("forward:/typeManager.htm");
		view.addObject("pathFlag", "addBigType");
		return view; 
	}
	
	@RequestMapping(value="/deleteBigType.htm")
	public ModelAndView deleteBigType(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("forward:/typeManager.htm");
		String typeId = request.getParameter("typeId");
		this.financeService.deleteBigType(typeId);
		return view;
	}
	
	
	@RequestMapping(value="/addSmallType.htm")
	public ModelAndView addSmallType(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("forward:/typeManager.htm");
		String typeName = request.getParameter("typeName");
		String bigTypeId = request.getParameter("bigTypeId");
		System.out.println("typeName:"+typeName+",bigTypeId:"+bigTypeId);
		this.financeService.addSmallType(typeName, bigTypeId);
		view.addObject("pathFlag", "smallType");
		view.addObject("bigTypeId", bigTypeId);
		return view;
	}
	
	@RequestMapping(value="/deleteSmallType.htm")
	@ResponseBody
	public void deleteSmallType(HttpServletRequest request,
			HttpServletResponse response){
		String typeId = request.getParameter("typeId");
		this.financeService.deleteSmallType(typeId);
	}
	
	@RequestMapping(value="/addNewFinance.htm")
	public ModelAndView addNewFinance(HttpServletRequest request,
			HttpServletResponse response,FinanceHbmModel financeModel) throws ParseException{
		//handle the finance date.
		String dateTime = request.getParameter("dateTime");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date financeDate = format.parse(dateTime);
		//end
		String smallTypeId = request.getParameter("financeType");
		ModelAndView view = new ModelAndView("forward:/financeInput.htm");
		String financeNumber = request.getParameter("financeNumber");
		double money = Double.parseDouble(financeNumber);
		HttpSession session = request.getSession();
		UserHbmModel userModel =(UserHbmModel)session.getAttribute("user");
		financeModel.setMaker(userModel.getUuid());
		financeModel.setMoney(money);
		financeModel.setFinanceDate(financeDate);
		this.financeService.addNewFinance(financeModel,smallTypeId);
		return view;
	}
	
	@RequestMapping(value="/viewFinance.htm")
	public ModelAndView viewFinance(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("finance/financeView");
		String p_year = request.getParameter("year");
		String p_month = request.getParameter("month");
		String p_type = request.getParameter("type");
		Calendar date = Calendar.getInstance();
		if(ToolUtil.isEmpty(p_year)){
			p_year = String.valueOf(date.get(Calendar.YEAR));
		}
		if(ToolUtil.isEmpty(p_month)){
			p_month = String.valueOf(date.get(Calendar.MONTH));
		}
		if(ToolUtil.isEmpty(p_type)){
			p_type = "detail";
		}
		view.addObject("p_year", p_year);
		view.addObject("p_month", p_month);
		view.addObject("p_type", p_type);
		//build selected option of year and month
		List<Map<String,String>> yearList = new ArrayList<Map<String,String>>();
		for(int i=date.get(Calendar.YEAR)-5;i<date.get(Calendar.YEAR)+5;i++){
			Map<String,String> yearPara = new HashMap<String,String>();
			yearPara.put("yearValue",String.valueOf(i));
			yearList.add(yearPara);
		}
		view.addObject("yearList", yearList);
		//get data
		HashMap<String,String> paramList = new HashMap<String,String>();
		paramList.put("YEAR", p_year);
		paramList.put("MONTH", p_month);
		paramList.put("paging", "no");
		HttpSession httpSession = request.getSession();
		UserHbmModel userModel =(UserHbmModel)httpSession.getAttribute("user");
		paramList.put("MAKER", userModel.getUuid());
		if("detail".equals(p_type)){
			List<FinanceHbmModel> resultList = this.financeService.getAllFinanceItems(paramList);
			view.addObject("financeList", resultList);
		}else{
			Map<String,String> statistic = this.financeService.viewFinanceInfo(paramList);
			view.addObject("statistic", statistic);
		}
		return view;
	}
}
